#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: customlabel.py
# Author: Tomás Vírseda
# License: GPL v3
# Description:  Status bar displays a progress bar, message test and some others buttons

"""
"""
import os
import re
import threading
import time

import gobject
import gtk

#import utils
from vazaarlib.constants import GUI_DIR
from vazaarlib.ui.glade import Widgets



class StatusBarManager():
    """ status bar manager """

    #log = utils.get_logger("StatusBar")
    widget = {}
    working = True

    def __init__(self, gui):
        self.fraction = 0.1
        self.text = ''
        self.gui = gui
        self.gui.frontend['progressbar'] = self.gui.glade.widgets.get_widget("progressbar")
        self.gui.frontend['statusbar'] = self.gui.glade.widgets.get_widget("statusbar")
        self.gui.frontend['btncancelimport'] = self.gui.glade.widgets.get_widget("btnCancelImport")
        self.gui.frontend['statuslabel'] = self.gui.glade.widgets.get_widget("lblStatusBar")
        self.gui.frontend['btnabout'] = self.gui.glade.widgets.get_widget("btnAbout")
        self.gui.frontend['tgbmenu'] = self.gui.glade.widgets.get_widget("tgbMenu")
        self.gui.frontend['connections'] = {
            'btnCancelImport/clicked' : self.cancel_import
        }
        #connect signals
        for wid_con, func in self.gui.frontend['connections'].iteritems():
            wid,con = wid_con.split('/')
            self.gui.glade[wid].connect(con,func)


    def update_progressbar(self, fraction, text):
        self.gui.frontend['progressbar'].set_fraction(fraction)
        self.gui.frontend['progressbar'].set_text(text)

    def update_statusbar(self, message):
        try:
            self.gui.frontend['statuslabel'].set_markup(message)
        except Exception, error:
            self.log.error(error)

    def cancel_import(self, widget, data=None):
        self.gui.actions.cancel_import()


    def quit(self, *args):
        self.gui.actions.stop()


    def set_text(self, label):
        try:
            #FIXME: find and delete annoying characters
            self.gui.frontend['statuslabel'].set_markup(label)
        except Exception, error:
            self.log.error(error)


    def get_text(self):
        return self.gui.frontend['statuslabel'].get_text()

    def get_widget(self):
        return self.label



